package model

import "gorm.io/gorm/clause"

type Net struct {
	Id      string	`xml:"-" json:"id" gorm:"column:id;primary_key;not null;index"`
	Isdhcp  *int	`xml:"-" json:"isdhcp"`
	Ipaddr  string	`xml:"-" json:"ipaddr"`
	Gateway string	`xml:"-" json:"gateway"`
	Netmask string	`xml:"-" json:"netmask"`
	Dns     string	`xml:"-" json:"dns"`
}
func FindNets(filter *Net)[]Net{
	var kps []Net
	if err:=sqldb.Find(&kps,filter).Error;err != nil{
		return nil
	}else{
		return kps
	}
}
func DeleteNets()(int64,error){
	var kps []Net
	sqldb.Find(&kps)
	index :=0
	for i:=0; i < len(kps); i++{
		if _,err := kps[i].Delete();err != nil{
			return int64(index),err
		}
		index += 1
	}
	return int64(index),nil
}
func (p* Net) Insert()error{
	return sqldb.Create(p).Error
}
func (p* Net) Save()(int64,error){
	ret := sqldb.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "id"}},
		DoUpdates: clause.AssignmentColumns([]string{"isdhcp", "ipaddr","gateway","netmask","dns"}),
	}).Updates(p)
	return ret.RowsAffected,ret.Error
}
func (p* Net) Delete()(int64,error){
	db :=sqldb.Where(&Net{Id:p.Id}).Delete(&Net{})
	return db.RowsAffected,db.Error
}